package com.teamclub.app.sservices.clientmanager

import io.ebean.{Ebean, ExpressionList}
import com.teamclub.app.sforms.AppClientVersionForm
import com.teamclub.domain.system.AppClientVersion
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.F
import org.apache.commons.lang3.StringUtils
import org.springframework.stereotype.Service

@Service
class AppClientVersionService {
  def expr(where: ExpressionList[AppClientVersion], form: AppClientVersionForm): Unit = {
    if(StringUtils.isNotBlank(form.key)){
      where.eq("key", form.key)
    }
    where.orderBy("createTime desc")
  }

  def getNewestVersion(form: AppClientVersionForm): F.Either[AppClientVersion, ErrorCode] = {
    if(StringUtils.isNotBlank(form.key)) {
      return F.Either.Right(ErrorCode.ref("需要传递key"))
    }
    val where = Ebean.find(classOf[AppClientVersion]).where()
    expr(where, form)
    val instance = where.setMaxRows(1).findOne()
    if(instance == null) {
      return F.Either.Right(ErrorCode.ref("未查询对应的实体"))
    }
    F.Either.Left(instance)
  }

}
